package cn.blossoming.service.dto;

import cn.blossoming.dao.domain.*;
import cn.blossoming.service.base.consts.SessionConst;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.stream.Collectors;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Tips {

    private Integer id;
    private String title;
    private Integer readNum;
    private Integer commentNum;
    private Integer praiseNum;
    private boolean isPraise = false;
    private boolean isMyself = false;
    private String remark;

    public Tips(TipsEntity tips) {
        this.id = tips.getId();
        this.title = tips.getTitle();
        this.readNum = tips.getReadNum();
        this.commentNum = tips.getCommentNum();
        this.praiseNum = tips.getPraiseNum();
        this.remark = tips.getRemark();
    }

    private User user;
    private List<TipsComment> tipsComments;


    private List<TipsPicture> tipsPictures;


    private List<TipsPraise> tipsPraises;


    private List<TipsRead> tipsReads;


    private List<String> tipsTags;

    public void setTipsTags(List<TipsTagEntity> entities) {
        this.tipsTags = entities.stream().map(entity -> entity.getTag().getName()).collect(Collectors.toList());
    }

    public static List<TipsComment> TipsEntityComments2TipsComments(List<TipsCommentEntity> entities) {
        Integer userId = SessionConst.getCurrentUserId();
        return entities.stream()
                .map(entity -> {
                    TipsComment comment = new TipsComment(entity);
                    comment.setTipsComments(entity.getTipsComments().stream().map(TipsComment::new).collect(Collectors.toList()));
                    comment.setUser(User.builder().id(entity.getUser().getId()).name(entity.getUser().getName()).build());
                    comment.setMyself(comment.getUser().getId().equals(userId));
                    return comment;
                }).collect(Collectors.toList());
    }
}
